#!/usr/bin/python
import MuConnection, MuScaleApi, time, sys, getopt, simplejson

USERNAME = 'admin'
PASSWORD = ''
MU = ''

#get the username and password
args = sys.argv[0:]
try:
    options, remainder = getopt.getopt(sys.argv[1:], "u:p:i:", ["username=", "password=","ipaddress="])
except getopt.GetoptError, err:
    # print help information and exit:
    print str(err) # will print something like "option -a not recognized"
    usage()
    sys.exit(2)
for o, p in options:
    if o in ("-u","--username"):
     	USERNAME = p
    elif o in ("-p", "--password"):
     	PASSWORD = p
    elif o in ("-i", "--ipaddress"):
        MU = p

custome = False        

doTest = 'doTest'
doRTSP = 'doRTSP'
doSIP = 'doSIP'
doHTTP = 'doHTTP'
doFTP = 'doFTP'
doSUNRPC = 'doSUNRPC'
doMSRPC = 'doMSRPC'

TEST = doSUNRPC


if TEST==doTest:
	myScenarioMuslFile = "Tutorial_ HTTP GET.msl"
	myHosts = {'host_0':'a1/192.168.100.221','host_1':'a2/192.168.100.231'}
	myCsvFile = None
	myDelay = 0
	myPattern = {"iterations":1,"intervals":[{"iterations":1,"end":100,"start":10,"duration":60}]}
	myVolume = 100
elif TEST==doRTSP:
	myScenarioMuslFile = "rtsp_scale.msl"
	myHosts = {'host_0':'b1/192.168.200.128','host_1':'b2/192.168.200.192'}
	#myHosts = {'host_0':'b1/192.168.200.220','host_1':'b2/192.168.200.230'}
	myCsvFile = 'rtsp_scale.csv'
	myDelay = 0
	myPattern = {"iterations":1,"intervals":[{"iterations":1,"end":1000,"start":50,"duration":60}]}
	myVolume = 100
elif TEST==doSIP:
	print "TODO doSIP"
elif TEST==doHTTP:
	print "TODO doHTTP"
elif TEST==doFTP:
	print "TODO doFTP"
elif TEST==doSUNRPC:
	myScenarioMuslFile = "nfstest_4_scale_1IP.msl"
	myHosts = {'host_0':'b1/192.168.200.128','host_1':'b2/192.168.200.192'}
	myCsvFile = 'nfstest_4_scale_1IP.csv'
	myDelay = 0
	myPattern = {"iterations":1,"intervals":[{"iterations":1,"end":1000,"start":1,"duration":60},{"iterations":1,"end":10,"start":1000,"duration":60}]}
	myVolume = 100
elif TEST==doMSRPC:
	print "TODO doMSRPC"

file = open(myScenarioMuslFile, 'r')
myMuslData = file.read()
file.close()
if myCsvFile==None:
	myCSV = "\n"
else:
	file = open(myCsvFile, 'r')
	myCSV = file.read()
	file.close()

# establish conection to the Mu
myMuConnection = MuConnection.MuConnection(MU,USERNAME,PASSWORD)

# establish scale player API instance
myScalePlayer = MuScaleApi.MuScaleApi(myMuConnection)

# set verbose output to console
MuConnection.VERBOSE = False
myScalePlayer.VERBOSE = False

# get a new scale player
muScaleUuid = ''
print '------------ new scale player ------------------'
response = myScalePlayer.newSession()
# set uuid for use later
muScaleUuid = response.getUUID()

# an example of listing the scale players on hte system.
if True:
	print '------------ list scale player ------------------'
	response = myScalePlayer.listActiveScalePlayers()
	muScaleUuid = response.getUUID()
	print 'My Scale UUID: ' + muScaleUuid

# an example of a status before scale player has been loalded with test data
print '------------ status data from non inialized scale player ------------------'
response = myScalePlayer.getScalePlayerStatus(muScaleUuid)
status = response.getRetrievedObject()
print str(status)
	


myScaleConfig = MuScaleApi.scaleConfig()
myScaleConfig.setScenarioMusl(myMuslData)
myScaleConfig.setHosts(myHosts)
myScaleConfig.setCsv(myCSV)
myScaleConfig.setDelay(myDelay)
myScaleConfig.setPattern(myPattern)
print '------------ example scale config  ------------------'
print myScaleConfig
file = open('myLastScaleJson.json', 'w')
file.write(str(myScaleConfig))
file.close()

print '------------ init and verify scale player ------------------'
response = myScalePlayer.initScalePlayer(myScaleConfig,muScaleUuid)
print '------------ raw response from initalized scale player ------------------'
print response.getRawResponse()




if custome:
	print '------------ set inital volume and delay on scale player ------------------'
	myScalePlayer.updateScalePatern(muScaleUuid,1,100)
	time.sleep(2)
	
	response = myScalePlayer.getScalePlayerStatus(muScaleUuid)
	status = response.getRetrievedObject()
	print str(status)
	time.sleep(1)
	
	print '------------ start scale player ------------------'
	myScalePlayer.startScalePlayer(myScaleConfig,muScaleUuid)
	time.sleep(3)

	print '------------ set scale player Volume to 100 ------------------'
	myScalePlayer.updateScalePatern(muScaleUuid,100)
	time.sleep(2)
	
	response = myScalePlayer.getScalePlayerStatus(muScaleUuid)
	status = response.getRetrievedObject()
	print str(status)
	

	print '------------ set scale player volume to 1000, and delay to 1000 ------------------'
	myScalePlayer.updateScalePatern(muScaleUuid,1000,1000)
	time.sleep(2)
	
	response = myScalePlayer.getScalePlayerStatus(muScaleUuid)
	status = response.getRetrievedObject()
	print str(status)
	

	print '------------ set scale player delay to 15000 ------------------'
	myScalePlayer.updateScalePatern(muScaleUuid,None,15000)
	time.sleep(2)
	
	response = myScalePlayer.getScalePlayerStatus(muScaleUuid)
	status = response.getRetrievedObject()
	print str(status)
	

	print '------------ set scale player volume to 5, and delay to 11000 ------------------'
	myScalePlayer.updateScalePatern(muScaleUuid,5,11000)
	time.sleep(2)
	
	response = myScalePlayer.getScalePlayerStatus(muScaleUuid)
	status = response.getRetrievedObject()
	print str(status)

	print '------------ set scale player volume to 1, and delay to 0 ------------------'
	myScalePlayer.updateScalePatern(muScaleUuid,1,0)
	time.sleep(2)
	
	response = myScalePlayer.getScalePlayerStatus(muScaleUuid)
	status = response.getRetrievedObject()
	print str(status)
	
	print '------------ stop scale player ------------------'
	myScalePlayer.stopScalePlayer(muScaleUuid)
	time.sleep(2)
	
else:
	print '------------ start scale player ------------------'
	myScalePlayer.startScalePlayer(myScaleConfig,muScaleUuid)
	time.sleep(3)

	print '------------ monitor scale player if patern is present ------------'
	loop = True
	count = 0
	while loop:
		time.sleep(2)
		response = myScalePlayer.getScalePlayerStatus(muScaleUuid)
		status = response.getRetrievedObject()
		print str(status)
		if response.hasError():
			loop = False
		else:
			loop = status.running
		if count <=40:
			loop = True
		count = count+1
		

print '------------ release scale player ------------------'
myScalePlayer.releaseScalePlayer(muScaleUuid)





